﻿Public Class CINNERLabelState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-IN") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CINNERLabelState.DoExecute")
        'MessageBox.Show("INNER_STATE")
        'CFileLog.WriteLog("CINNERLabelState.DoExecute START---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance
        Dim record As COrderRecord = pdaTask.WindowManager.CreateLabelList
        Dim checkInfo As CCheck = CCheck.GetIns

        'Dim test As COrderRecord = New COrderRecord
        'test.ItemCd = "1212"
        Try
            Dim app As CApplication = CApplication.GetInstance()
            Dim sql As CSQLite = app.m_MasterDB
            Dim masterDao As New CMasterDAO
            Dim trx As CTransaction = sql.GetTransaction

            Dim lblDataList As ArrayList = checkInfo.GetLabelDataList
            Dim uniDataList As ArrayList = checkInfo.GetUniqueDataList

            '親子ラベルデータをINSERTする============================>
            For i As Integer = 0 To lblDataList.Count - 1
                Dim ParentLblData As CCheck.LabelData = lblDataList(i)
                pdaTask.SetCd = ParentLblData.setCd
                ParentLblData.grpPattern = pdaTask.PatternList(0)

                If masterDao.InsertLblData(trx, ParentLblData) Then
                    '子ラベルのデータInsert
                    For j As Integer = 0 To ParentLblData.chdLabelInfo.Length - 1
                        Dim ChdLblData As CCheck.ChdLabelInfo = ParentLblData.chdLabelInfo(j)

                        ChdLblData.lbCate = "1"     ''-->ラベル照合のみは1    ラベル照合しなかったら0
                        ChdLblData.shCate = "0"     ''-->出荷照合のみは1    出荷照合しなかったら0

                        If masterDao.InsertChdLblData(trx, ChdLblData, pdaTask.PatternList(0)) Then

                        End If
                    Next
                End If
            Next
            'ユニークコードをINSERTする
            For j As Integer = 0 To uniDataList.Count - 1
                Dim uniLblData As CCheck.UniqueLblData = uniDataList(j)
                If masterDao.InsertUniqueData(trx, uniLblData) Then
                    'ADD
                    checkInfo.add_AllUniData(uniLblData.uniqueCd)
                    checkInfo.add_PartUniData(uniLblData.uniqueCd)
                End If
            Next

            pdaTask.Count = pdaTask.Count + 1
            With record
                .SetCd = pdaTask.SetCd
                .ListValue1 = pdaTask.Record1
                .ListValue2 = pdaTask.Record2
                .ListValue3 = pdaTask.Record3
                .SubinvoiceCd1 = pdaTask.SubInvoiceCd1
                .SubinvoiceCd2 = pdaTask.SubInvoiceCd2
                .SubinvoiceCd3 = pdaTask.SubInvoiceCd3
                .SubinvoiceCd4 = pdaTask.SubInvoiceCd4
                .Count = pdaTask.Count
                '’表示情報
                .ShiporderCd = ""
                .RowNumber = ""
                .OrderCd = pdaTask.OrderCd
                .SysItemCd = pdaTask.SysitemCd
                .ItemCd = pdaTask.ItemCd
                .InvoiceCd = pdaTask.InvoiceCd
                .ItemName = pdaTask.ItemName
                .OrderQty = pdaTask.OrderQty
                .BoxQty = 0
                .ActionQty = pdaTask.ActionQty '0 'Integer.Parse(pdaTask.InQty)
                .ActionBoxQty = 0 'Integer.Parse(pdaTask.InBoxQty)
                .ActionDateTime = ""
            End With

            'rollbackの機能
            checkInfo.add_PartLblData(pdaTask.SetCd)
            checkInfo.add_AllLblData(pdaTask.SetCd)

            'sql.RelaseTransaction(trx)
            'trx.Commit()
            checkInfo.InsertOk()
            '照合が終わったので取消されるように
            CCommon.FlagManage("LBL_SHOUGO", False)

            'もし、明細画面のリストは全部削除
            pdaTask.WindowManager.DelActionLbl()


            'trx.Rollback()

        Catch ex As Exception
            'MessageBox.Show("Inner:" & ex.Message)
        Finally

        End Try

        If pdaTask.EndFlg = "1" Then
            'MessageBox.Show("一つのセットが終わり")
            '一つのセットが終わり   
            CPersonal.initPdaTask(pdaTask)
            pdaTask.WindowManager.ClearActionLabel()
            '部分のSETCDは初期化
            checkInfo.initPartLblData()
            pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_031, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
        End If
     
        '画面設定
        'pdaTask.WindowManager.AddRecord(CPersonal.OrdetDataWindowSet(newCheckData, record))
        pdaTask.WindowManager.AddRecord(record)


        pdaTask.LabelCount = 1
        task.ProcessText = ""
        task.ActionCd = "LABEL"
        nextState = GetNextState(task)
        'CFileLog.WriteLog("CINNERLabelState.DoExecute END---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState.DoExit")
        Return True
    End Function


End Class
